package itsm.isperp.module.controller.knowledge;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.TreeNodeService;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.module.entity.knowledge.KngKnowledgeCategory;
import itsm.isperp.module.service.knowledge.KngKnowledgeCategoryService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.chanjar.weixin.common.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/knowledge/knowledgeCategory")
public class KngKnowledgeCategoryController extends
		TreeNodeController<KngKnowledgeCategory> {

	/**
	 * 
	 */
	@Autowired
	protected KngKnowledgeCategoryService kngKnowledgeCategoryService;

	@Override
	public TreeNodeService<KngKnowledgeCategory> getService() {
		return kngKnowledgeCategoryService;
	}

	@Override
	public String getPageTitle() {
		return "知识分类";
	}

	@RequestMapping("getTree")
	@ResponseBody
	@Override
	public List<KngKnowledgeCategory> getTree(String id, boolean asyncLoad,
			String parentId, boolean showRoot, HttpServletRequest req) {

		if (asyncLoad) {

			JdbcTemplate jdbcTemplate = ContextHolder
					.getSpringBean("jdbcTemplate");

			List<KngKnowledgeCategory> list = this.getService().getChildren(id,
					showRoot);

			if ("1".equals(req.getParameter("showNumber"))) {
				String sql = null;
				if (StringUtils.isEmpty(id)) {
					sql = "SELECT count(*) FROM kng_knowledge_item a INNER JOIN kng_knowledge_category b  ON a.kng_knowledge_category_id = b.id  ";

					KngKnowledgeCategory cRoot=list.get(0);
					int countRoot = jdbcTemplate.queryForObject(sql , Integer.class);
					cRoot.setName(cRoot.getName() + "(<label style='color:blue'>"
							+ countRoot + "</label>)");
					
					sql = "SELECT count(*) FROM kng_knowledge_item a INNER JOIN kng_knowledge_category b  ON a.kng_knowledge_category_id = b.id WHERE     b.treenode_path LIKE ";

					
					if (cRoot.getChildren() != null) {
						for (ITreeNode c : cRoot.getChildren()) {
							int count = jdbcTemplate.queryForObject(sql + "'%/"
									+ c.getId() + "/%'", Integer.class);

							c.setName(c.getName()
									+ "(<label style='color:blue'>" + count
									+ "</label>)");
						}
					}

				} else {
					sql = "SELECT count(*) FROM kng_knowledge_item a INNER JOIN kng_knowledge_category b  ON a.kng_knowledge_category_id = b.id WHERE     b.treenode_path LIKE ";

					for (KngKnowledgeCategory c : list) {
						int count = jdbcTemplate.queryForObject(
								sql + "'%/" + c.getId() + "/%'", Integer.class);

						c.setName(c.getName() + "(<label style='color:blue'>"
								+ count + "</label>)");
					}

				}
			}

			return list;

		}

		return this.getService().getTree(parentId, showRoot);
	}
}